
$(function() {
	$(".nav-ul li").eq(4).addClass("cur");
	if ($(".form-inline").hasClass('form-more')) {
		$('.btn-more').show()
	}
	$('.btn-clear-form').click(function() {
		$('.form-inline input').val('')
	})
	
	query(1);

	 $('#pageNum').dropdown()
	 $('#dropdown').on('shown.bs.dropdown', function () {
	     $('.dropdown-menu li').click(function () {
	         var str = $(this).children('a').html();
	         var size = $(this).children('a').children('label').text();
	         str += '<span class="caret"></span>';
	         $("#pageNum").html(str);
	         query(1);
	     })
	 });
	
});

function jumpQuery(){
	var page = $("#jump_page").val();
	var reg = /^\+?[1-9][0-9]*$/;//判断是否为正整数 
    var flag = reg.test(trim(page));
    if(!flag){
    	layer.alert("请输入正整数！", {
			icon : 3,
			offset: '100px'
		});
    }else{
    	var totalPages = $("#total_pages").text();
    	if(parseInt(page) > parseInt(totalPages)){
    		layer.alert("跳转页数不能超过总页数！", {
    			icon : 3,
    			offset: '100px'
    		});
    	}else{
    		query(page);
    	}
    }
}

function query(page){
	layer.load(4);
	$.ajax({
		type : "POST",
		url :  "cdnData/getCdnDataInfo",
		data : {
			"currentPage" : page,
			"cdnName" : $("#cdnName").val(),
			"cdnId" : $("#cdnId").val(),
			"enterprise" : $("#enterprise").val(),
			"userId" : $("#userId").val(),
			"loginUser" : $("#loginUser").val(),
			"userLevel" : $("#userLevel").val(),
			"pageSize" : $("#pageNum").children('strong').text()
		},
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var tread = data.datas;
				insertInfoTpl(tread, $('#tread'), $('#treadTpl'));
				var currentPage = data.currentPage;
				var totalPages = data.totalPages;
				var totalData = data.totalData;
				$("#total_data").text(totalData);
				$("#total_pages").text(totalPages);
				var options = {
					currentPage : currentPage, // 当前的请求页面。
					totalPages : totalPages, // 一共多少页。
					size : "normal", // 应该是页眉的大小。
					bootstrapMajorVersion : 3, // bootstrap的版本要求。
					alignment : "right",
					numberOfPages : 5, // 一页显示几个按钮
					itemTexts : function(type, page, current) { // 如下的代码是将页眉显示的中文显示我们自定义的中文。
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						case "page":
							return page;
						}
					},
					tooltipTitles : function(type, page, current) { // 提示框，当鼠标放上去给提示
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";

						}
					},
					onPageClicked : function(event, originalEvent, type, page) {
						layer.load(4);
						$.ajax({
							type : "POST",
							url : "cdnData/getCdnDataInfo",
							data : {
								"currentPage" : page,
								"cdnName" : $("#cdnName").val(),
								"cdnId" : $("#cdnId").val(),
								"userId" : $("#userId").val(),
								"enterprise" : $("#enterprise").val(),
								"loginUser" : $("#loginUser").val(),
								"userLevel" : $("#userLevel").val(),
								"pageSize" : $("#pageNum").children('strong').text()
							},
							dataType : "json",
							success : function(data) {
								if (data.flag) {
									layer.closeAll();
									var tread = data.datas;
									insertInfoTpl(tread, $('#tread'),$('#treadTpl'));
								}
							},
							error : function(data) {
								layer.closeAll();
							}
						});
					}
				}
				$('#pageLimit').bootstrapPaginator(options);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function linkPage(id, cdnId) {
	var loginUser = $("#loginUser").val();
	var userLevel = $("#userLevel").val();
	var userId = $("#userId").val();
	var htmlSrc = {
		subNum : 'cdnNetLink?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel,
		ipNum : 'cdnIpLink?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel,
		customerNum : 'cdnCustomerLink?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel,
		domainNum : 'cdnDomainLink?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel,
		houseNum : 'cdnHouseLink?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel,
		nodeNum : 'cdnNode?linkCdnId='+cdnId+"&userId="+userId+"&loginUser="+loginUser+"&userLevel="+userLevel
	}
	var iframeSrc = '';
	if (id == 'subNum') {
		iframeSrc = htmlSrc.subNum;
	}else if(id == 'ipNum'){
		iframeSrc = htmlSrc.ipNum;
	}else if(id == 'customerNum'){
		iframeSrc = htmlSrc.customerNum;
	}else if(id == 'domainNum'){
		iframeSrc = htmlSrc.domainNum;
	}else if(id == 'houseNum'){
		iframeSrc = htmlSrc.houseNum;
	}else if(id == 'nodeNum'){
		iframeSrc = htmlSrc.nodeNum;
	}
	var iframeStr = '<iframe src=' + iframeSrc+ ' frameborder="0"></iframe>';
	$('#modalBody').html(iframeStr);
}

function exportData(){
	layer.load(4);
	$.ajax({
		type : "POST",
		url : "cdnData/exportData",
		data : {
			"loginUser" : $("#loginUser").val(),
			"userLevel" : $("#userLevel").val()
		},
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var url = "cdnData/downFiles?path="+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function detailInfo(cdnId){
	layer.load(4);
	$.ajax({
		type : "POST",
		url : "cdnData/getDetailInfo",
		data : {
			"cdnId" : cdnId
		},
		dataType : "json",
		success : function(data) {
			if (data.flag) {
				layer.closeAll();
				var datas = data.datas;
				
				var detail = $("#detail_info");
				detail.empty();
			    var str  =  "<h4 class='table-title'>详细信息</h4>"+
							"<div class='table-responsive'>"+
							"  <table class='table table-bordered'>"+
							"     <thead><tr><th width='110'></th><th></th></tr></thead>"+
							"     <tbody>"+
							"         <tr><td>企业名称：</td><td>"+filter(datas.cdnName)+"</td></tr>"+
							"         <tr><td>企业地址：</td><td>"+filter(datas.cdnAdd)+"</td></tr>"+
							"         <tr><td>许可证号：</td><td>"+filter(datas.cdnId)+"</td></tr>"+
							"         <tr><td>企业联系人：</td><td>"+filter(datas.linkName)+"</td></tr>"+
							"         <tr><td>联系人电话：</td><td>"+filter(datas.mobile)+"</td></tr>"+
							/*"         <tr><td>地址及邮编：</td><td>"+filter(datas.emailAdd)+"</td></tr>"+*/
							/*"         <tr><td>首次对接时间：</td><td>"+filter(datas.firstDockingTime)+"</td></tr>"+*/
							"         <tr><td>首次对接时间：</td><td>"+filter(datas.timstamp)+"</td></tr>"+
							"     </tbody>"+
							"  </table>"+
							"</div>";   
				detail.append(str);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}


/**
 * [insertInfoTpl 插入 html信息]
 * @param  {[type]} data [渲染所需的数据]
 * @param  {[type]} dom  [加入页面中的哪个DOM节点]
 * @return {[type]}      [description]
 */
function insertInfoTpl(data, dom, tpl) {
	dom.empty();
    var infoTpl = tpl.html()
    var compiled = _.template(infoTpl)
    var html = ''
    _.forEach(data, function (item, index) {
        item.index = index + 1
        // console.log(item, index)
        html += compiled(item)
    })
    dom.append($(html))
}

function trim(str){ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

function filter(name){
	if(name == null || name == "null" || name == undefined){
		return "";
	}else{
		return name
	}
}
